package cn.ccimage.passwordkeeper;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/30.
 */


public class HistoryAdapter extends BaseAdapter {
    private Context context;
    // 点击索引：点击列表项；点击按钮；点击名字。
    protected final static int CLICK_INDEX_ITEM = 0;
    protected final static int CLICK_INDEX_COUNTRY = 1;
    protected final static int CLICK_INDEX_NAME = 2;
    // 记录数据列表
    private ArrayList<HistoryData> list;
    // 记录Activity中接受消息的Handler
    private Handler mHandle;
    private LayoutInflater mInflater;

    // 关键字
    protected final static String BUNDLE_KEY_LIDATA = "lidata";

    // 构造方法
    public HistoryAdapter(Context context, Handler handle, ArrayList<HistoryData> list) {
        this.context = context;
        mHandle = handle;
        this.list = new ArrayList<HistoryData>();
        this.mInflater = LayoutInflater.from(context);
        for(HistoryData data : list) {
            this.list.add(data);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CustomViewHolder holder = null;
        if (convertView == null) {
            holder=new CustomViewHolder();
            convertView = mInflater.inflate(R.layout.history_item, null);
            holder.title = (TextView)convertView.findViewById(R.id.title);
            holder.info = (TextView)convertView.findViewById(R.id.info);
            convertView.setTag(holder);

        }else {
            holder = (CustomViewHolder)convertView.getTag();
        }



        holder.title.setText(list.get(position).password);
        holder.info.setText(list.get(position).datetime);

        return convertView;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    public void UpdateData(ArrayList<HistoryData> list) {
        this.list.clear();
        for(HistoryData data : list) {
            this.list.add(data);
        }
    }
}
